//购物车添加数量
function add(btn) {
	//数量*价格
	//1、获取到当前数量+1，并更新input框
	var num = btn.parentElement.children[1].value;
	btn.parentElement.children[1].value = ++num;
	//2、获取单价,字符串
	var price = btn.parentElement.previousElementSibling.innerText
	//3、计算小计，并更新渲染
	var total = parseFloat(price) * num;
	btn.parentElement.nextElementSibling.innerText = total;
	
	//总计
	 calSum();
}
 
//购物车减去数量
function minus(btn) {
	//数量*价格
	//1、获取到当前数量+1，并更新input框
	var num = btn.parentElement.children[1].value;
	if (num == 0) {
		return;
	}
	btn.parentElement.children[1].value = --num;
	//2、获取单价,字符串
	var price = btn.parentElement.previousElementSibling.innerText
	//3、计算小计，并更新渲染
	var total = parseFloat(price) * num;
	btn.parentElement.nextElementSibling.innerText = total;
	
	//总计
	 calSum();
}
 
//全选和反选
var _all = document.getElementById("all");
var _sum = document.getElementById("sum");
//选出除了全选的复选框
var _boxes = document.querySelectorAll("input[type=checkbox]:not(#all)")
//全选
_all.onclick = function() {
	//checked:获取全框的选中状态
	//this:当前的点击对象
	var status = this.checked;
	_boxes.forEach(function(tag) {
		//下边三个选框跟全选框的状态保持一致
		tag.checked = status;
	})
	calSum();
}
 
//单选
_boxes.forEach(function(tag) {
	tag.onclick = function() {
		//过滤出所有被选中的复选框
		var chs = Array.from(_boxes).filter(function(item) {
			return item.checked == true;
		})
		//如果过滤出得选中的复选框长度等于所有复选框长度，说明全选
		_all.checked = chs.length === _boxes.length;
		
		//总计
		 calSum();
	}
 
})
 
 
//总计
function  calSum(){
	var sum=0;
	_boxes = document.querySelectorAll("input[type=checkbox]:not(#all)")
	var newBoxes=Array.from(_boxes).filter(function(tag){
		return tag.checked==true;
	})
	
	newBoxes.forEach(function(tag){
		sum+=parseFloat(tag.parentElement.parentElement.children[4].innerText)
	})
	
	_sum.innerText=sum;
 
}
